home *** CD-ROM | disk | FTP | other *** search
/ Isometric Game Programming with DirectX 7.0 / Isometric Game Programming.iso / source / chapter3 / isohex3_4 / isohex3_4.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-18  |  6.0 KB  |  242 lines

  1. /*****************************************************************************
  2. IsoHex3_4.cpp
  3. Ernest S. Pazera
  4. 18MAY2000
  5. Start a WIN32 Application Workspace, add in this file
  6. No other libs are required
  7. *****************************************************************************/
  8.  
  9. //////////////////////////////////////////////////////////////////////////////
  10. //INCLUDES
  11. //////////////////////////////////////////////////////////////////////////////
  12. #define WIN32_LEAN_AND_MEAN  
  13.  
  14. #include <windows.h>   
  15.  
  16. //////////////////////////////////////////////////////////////////////////////
  17. //DEFINES
  18. //////////////////////////////////////////////////////////////////////////////
  19. //name for our window class
  20. #define WINDOWCLASS "ISOHEX3"
  21. //title of the application
  22. #define WINDOWTITLE "IsoHex 3-4"
  23.  
  24. //bitmap sizes
  25. const int BITMAPHEIGHT=32;
  26. const int BITMAPWIDTH=64;
  27.  
  28. //////////////////////////////////////////////////////////////////////////////
  29. //PROTOTYPES
  30. //////////////////////////////////////////////////////////////////////////////
  31. bool Prog_Init();//game data initalizer
  32. void Prog_Loop();//main game loop
  33. void Prog_Done();//game clean up
  34.  
  35. //////////////////////////////////////////////////////////////////////////////
  36. //GLOBALS
  37. //////////////////////////////////////////////////////////////////////////////
  38. HINSTANCE hInstMain=NULL;//main application handle
  39. HWND hWndMain=NULL;//handle to our main window
  40. //memory dc
  41. HDC hdcMem=NULL;
  42. //bitmaps
  43. HBITMAP hbmNew=NULL;
  44. HBITMAP hbmOld=NULL;
  45.  
  46. //////////////////////////////////////////////////////////////////////////////
  47. //WINDOWPROC
  48. //////////////////////////////////////////////////////////////////////////////
  49. LRESULT CALLBACK TheWindowProc(HWND hwnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
  50. {
  51.     //which message did we get?
  52.     switch(uMsg)
  53.     {
  54.     case WM_LBUTTONDOWN:
  55.         {
  56.             //borrow dc from main window
  57.             HDC hdc=GetDC(hWndMain);
  58.  
  59.             //blit from the memory dc to the window's dc
  60.             BitBlt(hdc,LOWORD(lParam)-BITMAPWIDTH/2,HIWORD(lParam)-BITMAPHEIGHT/2,BITMAPWIDTH,BITMAPHEIGHT,hdcMem,0,0,SRCCOPY);
  61.  
  62.             //return the borrowed dc to the system
  63.             ReleaseDC(hWndMain,hdc);
  64.  
  65.             //handled, so return 0
  66.             return(0);
  67.         }break;
  68.     case WM_DESTROY://the window is being destroyed
  69.         {
  70.  
  71.             //tell the application we are quitting
  72.             PostQuitMessage(0);
  73.  
  74.             //handled message, so return 0
  75.             return(0);
  76.  
  77.         }break;
  78.     case WM_PAINT://the window needs repainting
  79.         {
  80.             //a variable needed for painting information
  81.             PAINTSTRUCT ps;
  82.             
  83.             //start painting
  84.             HDC hdc=BeginPaint(hwnd,&ps);
  85.  
  86.             /////////////////////////////
  87.             //painting code would go here
  88.             /////////////////////////////
  89.  
  90.             //end painting
  91.             EndPaint(hwnd,&ps);
  92.                         
  93.             //handled message, so return 0
  94.             return(0);
  95.         }break;
  96.     }
  97.  
  98.     //pass along any other message to default message handler
  99.     return(DefWindowProc(hwnd,uMsg,wParam,lParam));
  100. }
  101.  
  102.  
  103. //////////////////////////////////////////////////////////////////////////////
  104. //WINMAIN
  105. //////////////////////////////////////////////////////////////////////////////
  106. int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nShowCmd)
  107. {
  108.     //assign instance to global variable
  109.     hInstMain=hInstance;
  110.  
  111.     //create window class
  112.     WNDCLASSEX wcx;
  113.  
  114.     //set the size of the structure
  115.     wcx.cbSize=sizeof(WNDCLASSEX);
  116.  
  117.     //class style
  118.     wcx.style=CS_OWNDC | CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
  119.  
  120.     //window procedure
  121.     wcx.lpfnWndProc=TheWindowProc;
  122.  
  123.     //class extra
  124.     wcx.cbClsExtra=0;
  125.  
  126.     //window extra
  127.     wcx.cbWndExtra=0;
  128.  
  129.     //application handle
  130.     wcx.hInstance=hInstMain;
  131.  
  132.     //icon
  133.     wcx.hIcon=LoadIcon(NULL,IDI_APPLICATION);
  134.  
  135.     //cursor
  136.     wcx.hCursor=LoadCursor(NULL,IDC_ARROW);
  137.  
  138.     //background color
  139.     wcx.hbrBackground=(HBRUSH)GetStockObject(BLACK_BRUSH);
  140.  
  141.     //menu
  142.     wcx.lpszMenuName=NULL;
  143.  
  144.     //class name
  145.     wcx.lpszClassName=WINDOWCLASS;
  146.  
  147.     //small icon
  148.     wcx.hIconSm=NULL;
  149.  
  150.     //register the window class, return 0 if not successful
  151.     if(!RegisterClassEx(&wcx)) return(0);
  152.  
  153.     //create main window
  154.     hWndMain=CreateWindowEx(0,WINDOWCLASS,WINDOWTITLE, WS_BORDER | WS_SYSMENU | WS_VISIBLE,0,0,320,240,NULL,NULL,hInstMain,NULL);
  155.  
  156.     //error check
  157.     if(!hWndMain) return(0);
  158.  
  159.     //if program initialization failed, then return with 0
  160.     if(!Prog_Init()) return(0);
  161.  
  162.     //message structure
  163.     MSG msg;
  164.  
  165.     //message pump
  166.     for(;;)    
  167.     {
  168.         //look for a message
  169.         if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
  170.         {
  171.             //there is a message
  172.  
  173.             //check that we arent quitting
  174.             if(msg.message==WM_QUIT) break;
  175.             
  176.             //translate message
  177.             TranslateMessage(&msg);
  178.  
  179.             //dispatch message
  180.             DispatchMessage(&msg);
  181.         }
  182.  
  183.         //run main game loop
  184.         Prog_Loop();
  185.     }
  186.     
  187.     //clean up program data
  188.     Prog_Done();
  189.  
  190.     //return the wparam from the WM_QUIT message
  191.     return(msg.wParam);
  192. }
  193.  
  194. //////////////////////////////////////////////////////////////////////////////
  195. //INITIALIZATION
  196. //////////////////////////////////////////////////////////////////////////////
  197. bool Prog_Init()
  198. {
  199.     //borrow dc from main window
  200.     HDC hdc=GetDC(hWndMain);
  201.  
  202.     //create a memory dc
  203.     hdcMem=CreateCompatibleDC(hdc);
  204.  
  205.     //return dc to system
  206.     ReleaseDC(hWndMain,hdc);
  207.  
  208.     //load in the bitmap
  209.     hbmNew=(HBITMAP)LoadImage(NULL,"IsoHex3_4.bmp",IMAGE_BITMAP,0,0,LR_LOADFROMFILE);
  210.  
  211.     //select bitmap into memory dc
  212.     hbmOld=(HBITMAP)SelectObject(hdcMem,hbmNew);
  213.  
  214.     return(true);//return success
  215. }
  216.  
  217. //////////////////////////////////////////////////////////////////////////////
  218. //CLEANUP
  219. //////////////////////////////////////////////////////////////////////////////
  220. void Prog_Done()
  221. {
  222.     //restore old bitmap to memory dc
  223.     SelectObject(hdcMem,hbmOld);
  224.  
  225.     //delete bitmap
  226.     DeleteObject(hbmNew);
  227.  
  228.     //delete dc
  229.     DeleteDC(hdcMem);
  230. }
  231.  
  232. //////////////////////////////////////////////////////////////////////////////
  233. //MAIN GAME LOOP
  234. //////////////////////////////////////////////////////////////////////////////
  235. void Prog_Loop()
  236. {
  237.     ///////////////////////////
  238.     //main game logic goes here
  239.     ///////////////////////////
  240. }
  241.  
  242.